package cn.tj.service.impl;

import cn.tj.entity.*;
import cn.tj.mapper.NormalMapper;
import cn.tj.service.NormalService;
import cn.tj.util.Haha;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;

import java.time.Duration;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class NormalServiceImpl implements NormalService {

    @Autowired
    private NormalMapper normalMapper;

    @Override
    public List<Client> ClientList() {
        return normalMapper.ClientList();
    }

    @Override
    public List<Technician> TechnicianList() {
        return normalMapper.TechnicianList();
    }

    @Override
    public Room getRoomById(Integer id,Double roomPrice) {
        //获取房间信息
        Room room = normalMapper.getRoomById(id);

        if(room!=null) {
            //如果存在开房时间则代表开房了
            long hours=0;
            if (room.getCreateTime()!=null){
                //转成LocalDateTime类型
                LocalDateTime localDateTime = room.getCreateTime().toLocalDateTime();
                //调用方法获取小时差
                hours = new Haha().countTimeHours(localDateTime);
            }

            //获取技师单价
            Double technicianPrice=0.0;
            if(room.getTechnicianId()!=null){
                technicianPrice = normalMapper.getTechnicianPrice(room.getTechnicianId());
            }
            //获取该房食物价格
            Double foodSumPrice = normalMapper.getFoodSumPrice(room.getId());
            if(foodSumPrice==null)
                foodSumPrice=0.0;
            //房价
            Double roomSumPrice=roomPrice*hours;
            //总价
            Double money=(technicianPrice*hours)+foodSumPrice+roomSumPrice;
            //修改房间金额
            normalMapper.editMoney(room.getId(),money);

            //重新获取房间
            Room newRoom =normalMapper.getRoomById(id);

            return normalMapper.getRoomById(id);
        }else{
            return room;
        }
    }

    /**
     * 退房
     * @param room
     */
    @Override
    public void openRoom(Room room) {
        normalMapper.openRoom(room);
    }

    @Override
    public void offRoom(Integer id) {
        //重置room信息
        normalMapper.offRoom(id);
        //根据房间id清空食物单的相关数据
        normalMapper.clearFoodOrderByRid(id);
    }

    @Override
    public List<Food> foodList() {
        return normalMapper.foodList();
    }

    @Override
    public void addFoodOrder(Integer roomId, Integer foodId) {
        normalMapper.addFoodOrder(roomId,foodId);
    }

    @Override
    public void addOrder(Room room) {
        //获取时差
        //转成LocalDateTime类型
        LocalDateTime localDateTime = room.getCreateTime().toLocalDateTime();
        //调用方法获取小时差
        Long hours = new Haha().countTimeHours(localDateTime);

        //获取本房间各小吃销量，给小吃表添加上去
        List<GetFoodsByRid> foodsByRid = normalMapper.getFoodsByRid(room.getId());
        for (GetFoodsByRid f : foodsByRid) {
            normalMapper.addsales(f.getId(),f.getCount());
        }

        //获取技师部分价格
        double technicianMoney = normalMapper.getTechnicianPrice(room.getTechnicianId()) * hours;
        //计入技师业绩
        normalMapper.editTechnician(room.getTechnicianId(),hours,technicianMoney);

        //客户余额扣款
        Client client = normalMapper.getClientById(room.getClientId());
        if(client.getVip()==1){//判断是否为会员
            //会员8折
            normalMapper.editClientMoney(room.getClientId(),(room.getMoney()*0.8));
        }else {
            normalMapper.editClientMoney(room.getClientId(),room.getMoney());
        }

        //退房,重置房间信息
        offRoom(room.getId());

        //新增订单
        normalMapper.addOrder(new Order(null,room.getClientId(),room.getTechnicianId(),room.getMoney(),null));
    }

    @Override
    public Client getClientById(Integer clientId) {
        return normalMapper.getClientById(clientId);
    }

    @Override
    public List<GetFoodsByRid> getFoodsByRid(Integer id) {
        return normalMapper.getFoodsByRid(id);
    }

    @Override
    public PageBean ClientListByPage(Integer currentPage, Integer pageSize, String name, Integer vip) {
        //创建pageBean对象
        PageBean<Object> pb = new PageBean<>();
        //开启分页查询
        PageHelper.startPage(currentPage,pageSize);
        //调用mapper
        List<Client> cList = normalMapper.ClientListByPage(name,vip);
        //分页插件提供的page类可以得到当前页数据和总记录条数
        Page p= (Page) cList;

        pb.setTotal(p.getTotal());
        pb.setItems(p.getResult());

        return pb;
    }

    @Override
    public Map<String,Object> getClientVipCount() {
        //获取客户列表
        List<Client> cList = normalMapper.ClientList();
        Integer vipYes=0;
        Integer vipNo=0;
        //等到会员与非会员数量
        for(Client client:cList){
            if(client.getVip()==0){
                vipNo++;
            }else{
                vipYes++;
            }
        }
        //放入map
        Map<String,Object> map=new HashMap<>();
        map.put("vipYes",vipYes);
        map.put("vipNo",vipNo);
        return map;
    }

    @Override
    public void deleteClientById(Integer id) {
        normalMapper.deleteClientById(id);
    }


    @Override
    public void editClientById(Client client) {
        //判断充值金额是否大于1000，大于则升级成会员
        if(client.getMoney()>=1000)
            client.setVip(1);
        normalMapper.editClientById(client);
    }

    @Override
    public void addClient(Client client) {
        //判断充值金额是否大于1000，大于则升级成会员
        if(client.getMoney()>=1000){
            client.setVip(1);
        }else{
            client.setVip(0);
        }
        normalMapper.addClient(client);
    }

    @Override
    public List<Order> orderList() {
        return normalMapper.orderList();
    }

}
